package com.kang.confirm;

import com.kang.utils.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author: zhangkangkang
 * @Description: 异步消息确认，效率最高
 * @date 2021/8/3 11:43
 */
public class Confirm3 {
    public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("tx43", false, false, false, null);
        //开启确认模式
        channel.confirmSelect();
        for (int i = 0; i < 10; i++) {
            channel.basicPublish("", "tx43", MessageProperties.TEXT_PLAIN, "zsf".getBytes());
        }

        channel.addConfirmListener(new ConfirmListener() {
            @Override
            public void handleAck(long l, boolean b) throws IOException {
                System.out.println("确认：" + l + "====>" + b);
            }

            @Override
            public void handleNack(long l, boolean b) throws IOException {
                System.out.println("未确认：" + l + "====>" + b);
            }
        });
    }
}
